package com.pandora.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisConnectionException;

import java.util.Map;

/**
 * @author developer
 */
public class JedisUtil {
    private static Logger logger = LoggerFactory.getLogger(JedisUtil.class);

    private static Map<String, JedisPool> multiJedisPoolMap = SpringContextHolder.getBean("multiJedisPoolMap");

    /**
     * 方法描述:从连接池获取jedis对象
     */
    private static Jedis getResource(String redisPoolKey){
        //声明jedis对象
        Jedis jedis = null;
        try{
            //从pool中获取jedis对象
            jedis = multiJedisPoolMap.get(redisPoolKey).getResource();
        }catch (JedisConnectionException ex) {
            logger.error("[JedisUtil:getResource]:get jedis object failed.message:\n"+ex.getMessage());
        }
        return jedis;
    }

    /**
     * 释放资源
     * @param redisPoolKey redis连接
     * @param jedis 当前连接client对象
     */
    private static void returnResource(String redisPoolKey, Jedis jedis) {
        JedisPool jedisPool = multiJedisPoolMap.get(redisPoolKey);
        if(jedisPool != null && jedis != null){
            jedis.close();
        }
    }

    /**
     * 获取缓存
     * @param key 键
     * @return 值
     */
    public static String get(String redisPoolKey, String key) {
        String value = null;
        Jedis jedis = null;
        try {
            jedis = getResource(redisPoolKey);
            if(jedis == null){
                return null;
            }
            if (jedis.exists(key)) {
                value = jedis.get(key);
                value = StringUtils.isNotBlank(value) && !"nil".equalsIgnoreCase(value) ? value : null;
            }
        } catch (Exception e) {
            logger.warn("get {} = {}", key, value, e);
        } finally {
            returnResource(redisPoolKey, jedis);
        }
        return value;
    }

    /**
     * 设置缓存
     * @param key 键
     * @param value 值
     * @param cacheSeconds 超时时间，0为不超时
     * @return string
     */
    public static String set(String redisPoolKey, String key, String value, int cacheSeconds) {
        String result = null;
        Jedis jedis = null;
        try {
            jedis = getResource(redisPoolKey);
            if(jedis == null){
                return null;
            }
            result = jedis.set(key, value);
            if (cacheSeconds != 0) {
                jedis.expire(key, cacheSeconds);
            }
        } catch (Exception e) {
            logger.warn("set {} = {}", key, value, e);
        } finally {
            returnResource(redisPoolKey, jedis);
        }
        return result;
    }

    /**
     * 执行redis的LPOP命令
     * @param redisPoolKey redis连接
     * @param key 主键
     * @return string
     */
    public static String executeLpop(String redisPoolKey, String key){
        String result = null;
        Jedis jedis = null;
        try {
            jedis = getResource(redisPoolKey);
            if(jedis == null){
                return null;
            }
            result = jedis.lpop(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            returnResource(redisPoolKey, jedis);
        }
        return result;
    }

    public static Long rpush(String redisPoolKey, String key, String value){
        Long result = null;
        Jedis jedis = null;
        try {
            jedis = getResource(redisPoolKey);
            if(jedis == null){
                return null;
            }
            result = jedis.rpush(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            returnResource(redisPoolKey, jedis);
        }
        return result;
    }
}
